package com.frxuexi.department.viewmodel

import com.frxuexi.department.intent.DepartIntent
import com.frxuexi.department.repository.DepartRepository
import com.frxuexi.lib_base.ApiResponse
import com.frxuexi.lib_base.BaseViewModel
import com.frxuexi.lib_base.RequestType
import com.frxuexi.lib_base.UiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import javax.inject.Inject

@HiltViewModel
class DepartViewModel @Inject constructor(private val departRepository: DepartRepository)
    :BaseViewModel<DepartIntent,UiState<Any>>(){
    override fun handlerIntent(intent: DepartIntent) {
        when(intent){
            is DepartIntent.getDeparts->{
                val map = mutableMapOf<String, Int>()
                map["userId"]=1
                map["page"]=2
                httpRequest(departRepository.getDepartments(map) as Flow<ApiResponse<Any>>)
            }
            is DepartIntent.deleteDoc->{
                val map = mutableMapOf<String, Int>()
                map["FileId"]=intent.fid
                httpRequest(departRepository.deleteDoc(map) as Flow<ApiResponse<Any>>,RequestType.DELETE)
            }
            is DepartIntent.getLocalDoc->{
                scanLocal(intent.path)
            }
            is DepartIntent.uploadDoc->{
                httpRequest(departRepository.uploadDoc(intent.name,intent.path) as Flow<ApiResponse<Any>>,RequestType.UPLOAD_FILE)
            }
            is DepartIntent.addDoc->{
                val map = hashMapOf<String, Any>(Pair("path", intent.path),Pair("name", intent.name),Pair("userId",1))
                httpRequest(departRepository.addDoc(map) as Flow<ApiResponse<Any>>,RequestType.UPLOAD_ASYNC)
            }
            else -> {}
        }
    }
}